package com.zhang.family_doctor.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.entity.Tag;
import com.zhang.family_doctor.entity.Team;
import com.zhang.family_doctor.mapper.TagMapper;
import com.zhang.family_doctor.service.TagService;
import com.zhang.family_doctor.utils.CommonUtils;
import com.zhang.family_doctor.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    /**
     * 新增标签
     * @param tag
     * @param request
     * @return
     */
    @Override
    public R<String> save(Tag tag, HttpServletRequest request) {

        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);
        tag.setId(CommonUtils.genUUID());
        tag.setNumber(CommonUtils.genUUID());
        tag.setAdded(getName);
        //新增默认启用
        tag.setState(1);
        tag.setCreateTime(new Date());
        tag.setUpdateTime(new Date());

        tagMapper.save(tag);

        return R.success("新增成功！");
    }

    /**
     * 修改标签
     * @param tag
     * @param request
     * @return
     */
    @Override
    public R<String> update(Tag tag, HttpServletRequest request) {

        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        tag.setAdded(getName);
        tag.setUpdateTime(new Date());
        tagMapper.update(tag);

        return R.success("修改成功！");
    }

    /**
     * 分页查询标签
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<PageInfo<Tag>> getList(Integer pageNum, Integer pageSize, String name) {

        PageHelper.startPage(pageNum, pageSize);
        List<Tag> tagList = tagMapper.getList(name);
        PageInfo<Tag> tagPageInfo = new PageInfo<>(tagList);

        return R.success(tagPageInfo);
    }

    /**
     * 根据id删除标签
     * @param id
     * @return
     */
    @Override
    public R<String> delete(Integer id) {

        tagMapper.delete(id);

        return R.success("删除成功！");
    }

    /**
     * 启用或禁用标签
     * @param id
     * @param state
     * @return
     */
    @Override
    public R<String> enableOrDisable(Integer id, Integer state) {

        tagMapper.enableOrDisable(id,state);
        return R.success("修改成功！");
    }


//    public static void main(String[] args) {
//        String aa=null;
//        String b="";
//        String c ="  ";
//        int a= (c.trim()=="")?1:0;
//
//        System.out.println();
//    }
}
